<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 排片信息管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        class="handle-del mr10"
                        @click="addbtn()"
                >添加
                </el-button>
                <el-button
                        type="primary"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="delAllSelection"
                >批量删除
                </el-button>
                <el-select v-model="query.cinemahallid" class="handle-select mr10" style="width: 310px">
                    <el-option v-for="h in halls" :value="h.cinemahallid"
                               :label="h.cinemahallname+'-'+h.cinemaHallType.cinemahalltypename"></el-option>
                </el-select>
                <el-date-picker
                        class="handle-select mr10"
                        style="width: 150px"
                        v-model="query.startDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期"
                        :picker-options="pickerOptions0">
                </el-date-picker>&nbsp;
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>

            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="scheduleid" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="movieDetailsDTO.moviechinesename" label="电影名"></el-table-column>
                <el-table-column label="电影厅">
                    <template slot-scope="scope">
                        {{scope.row.cinemaHallDTO.cinemahallname}}({{scope.row.cinemaHallDTO.cinemaHallType.cinemahalltypename}})
                    </template>
                </el-table-column>
                <el-table-column prop="movieDetailsDTO.movieorigin" label="语言版本"></el-table-column>
                <el-table-column prop="showtime" label="放映时间"></el-table-column>
                <el-table-column prop="viewingdate" label="观看日期"></el-table-column>
                <el-table-column label="电影价格">
                    <template slot-scope="scope">￥{{scope.row.movieprice}}</template>
                </el-table-column>


                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑
                        </el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        layout="total,  prev, pager, next, jumper"

                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form2" label-width="70px">
                <el-form-item label="　ID　">
                    <el-input v-model="form2.scheduleid" disabled style="width: 310px"></el-input>
                </el-form-item>
                <el-form-item label="电影　名">
                    <el-select v-model="form2.movieid" placeholder="" class="handle-select mr10" style="width: 310px">
                        <el-option v-for="m in movieDetailsList" :value="m.movieid"
                                   :label="m.moviedetails.moviechinesename"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="语言版本">
                    <el-input v-model="form2.movieorigin" disabled style="width: 310px"></el-input>
                </el-form-item>

                <el-form-item label="影　　厅">
                    <el-select v-model="form2.cinemahallid" class="form-control mr10" style="width: 310px">
                        <template v-for="h in halls">
                            <el-option :value="h.cinemahallid"
                                       :label="h.cinemahallname+'-'+h.cinemaHallType.cinemahalltypename"></el-option>
                        </template>
                    </el-select>
                </el-form-item>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="价　　格">
                            <el-input v-model="form2.movieprice" style="width: 310px"
                                      onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] :''"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="放映时间">
                            <el-date-picker
                                    class="handle-select mr10"
                                    style="width: 310px"
                                    v-model="alltime"
                                    type="datetime"
                                    format="yyyy-MM-dd HH:mm:ss"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    placeholder="选择时间"
                                    :picker-options="pickerOptions1">
                            </el-date-picker>&nbsp;
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!--添加弹出框-->
        <el-dialog title="添加" :visible.sync="addSchedule" width="30%">
            <el-form ref="form" :model="addform" label-width="70px">

                <el-form-item label="电影　名">
                    <el-select v-model="addform.indexid" @change="selectmovie" class="handle-select mr10"
                               style="width: 310px">
                        <el-option v-for="(m,index) in movieDetailsList" :value="index"
                                   :label="m.moviedetails.moviechinesename"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="电影厅">
                    <el-select v-model="addform.cinemahallid" class="handle-select mr10" style="width: 310px">
                        <el-option v-for="h in halls" :value="h.cinemahallid"
                                   :label="h.cinemahallname+'-'+h.cinemaHallType.cinemahalltypename"></el-option>
                    </el-select>
                </el-form-item>
                <el-row>
                    <el-form-item label="语言版本">
                        <el-input v-model="addform.movieorigin" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="上映日期">
                        <el-input v-model="addform.leasedata" disabled></el-input>
                    </el-form-item>
                    <el-col :span="12">
                        <el-form-item label="价　　格">
                            <el-input v-model="addform.movieprice"
                                      onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="放映时间">
                    <el-date-picker
                            class="handle-select mr10"
                            style="width: 310px"
                            v-model="alltime"
                            type="datetime"
                            format="yyyy-MM-dd HH:mm:ss"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择时间"
                            :picker-options="pickerOptions1">
                    </el-date-picker>&nbsp;
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addSchedule = false">取 消</el-button>
                <el-button type="primary" @click="confirmAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {fetchData} from '../../api/index';

    export default {
        name: 'Increase_row_piece',
        data() {
            return {
                query: {
                    cinemahallid: '',
                    pageIndex: 1,
                    pageSize: 10,
                    startDate: ''
                },
                addform: {
                    indexid: '',
                    movieid: "",
                    movieorigin: "",
                    cinemahallid: "",
                    movieprice: "",
                    showtime: "",
                    viewingdate: "",
                    leasedata: ''
                },
                form2: {
                    movieorigin: ''
                },
                alltime: '',
                halls: [],
                movieDetailsList: [],
                currentTotal: '',
                schedule: [],
                tableData: [],
                multipleSelection: [],
                delList: [],
                moviechinesenameList: {},
                editVisible: false,
                addSchedule: false,
                pageTotal: 0,
                idx: -1,
                id: -1,
                pickerOptions0: {
                    disabledDate: (time) => {
                        // time.getTime() < Date.now()
                        return false;
                    }
                },
                pickerOptions1: {
                    disabledDate: (time) => {
                        if ( time.getTime() < Date.now()){
                            return true;
                        }

                        return false;
                    }
                },
            };
        },
        created() {
            this.axios.get("/api/schedule/listHall", null).then((data) => {
                if (data.data.status == "1") {
                    this.halls = data.data.halls
                }
            }),
                this.getData();
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            getData() {
                this.axios.get('/api/schedule/scheduleList', {
                    params: this.query
                }).then((res) => {
                    // console.log(res.data)
                    this.tableData = res.data.pageInfo.list;
                    this.pageTotal = res.data.pageInfo.total;
                    this.pages = res.data.pageInfo.pages;
                    this.movieDetailsList = res.data.movieList;
                    // console.log(this.movieDetailsList)
                })

            },
            // 触发搜索按钮
            handleSearch() {
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.axios.get("api/schedule/delete", {
                        params: {
                            scheduleid: row.scheduleid
                        }
                    }).then(res => {
                        /*console.log(res)*/
                        if (res.data) {
                            this.getData();
                            this.$message.success('删除成功!');
                        } else {
                            this.$message.error('删除失败!');
                        }
                    })
                }).catch(() => {
                    this.$message.info('已取消删除!');
                });

            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            //多选删除
            delAllSelection() {
                this.$confirm('确定要删除吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                },).then(() => {
                    var arrids = this.multipleSelection.map(item => item.scheduleid).join();
                    // console.log(arrids);
                    this.axios.post("api/schedule/deleteAll", {
                        arrids: arrids
                    }).then(res => {
                        if (res.data) {
                            this.$message.success(`删除成功`);
                            this.getData();
                        } else {
                            this.$message.error(`删除失败`);
                        }
                    })
                }).catch(() => {
                    this.$message.info('已取消删除!');
                });
            },
            // 编辑操作
            handleEdit(index, row) {
                let str = row.viewingdate.slice(0, 4) + "-" + row.viewingdate.slice(5, 7) + "-" + row.viewingdate.slice(8, 10);
                let date = new Date(str);
                if (new Date() >= date) {
                    // console.log(new Date()+"--"+date)
                    this.$message.error(`放映过期,记录不可编辑!`)
                    return;
                }
                this.axios.get("/api/schedule/findOrderByScheduleId", {
                    params: {
                        scheduleId: row.scheduleid
                    }
                }).then((data) => {
                    if (data.data != 0) {
                        this.$message.error("该场次票已售出" + data.data + "张,不可再更改")
                        return;
                    }
                })
                this.form2 = JSON.parse(JSON.stringify(row));
                this.form2.movieorigin = row.movieDetailsDTO.movieorigin
                this.alltime = str + ' ' + this.form2.showtime;
                this.editVisible = true;
            },
            addbtn() {
                this.alltime = '';
                this.addSchedule = true;
            },
            //确定添加
            confirmAdd() {
                if (this.alltime == '' || this.addform.cinemahallid == ''
                    ||this.addform.movieprice == '' || this.addform.indexid == ''
                ) {
                    alert("表格填写不能为空!")
                    return;
                }
                var NumReg = /\d+(\.\d{0,2})?/
                if (!NumReg.test(this.addform.movieprice)){
                    alert("表格填写不能为空!")
                    return;
                }
                let str = this.alltime.split(' ')[0]
                let str1 = str.slice(0, 4) + "年" + str.slice(5, 7) + "月" + str.slice(8, 10) + "日";
                this.addform.viewingdate = str1
                this.addform.showtime = this.alltime.split(' ')[1]
                this.axios.post("api/schedule/addSchedule", this.addform).then((res) => {
                    if (res.data.sta == 'success') {
                        this.getData();
                        this.$message.success(res.data.msg);
                    } else {
                        this.$message.error(res.data.msg);
                    }
                    this.addSchedule = false;
                })
            },
            // 保存编辑
            saveEdit() {
                if (this.alltime == '' || this.form2.movieprice == '') {
                    alert("表格填写不能为空!")
                    return;
                }
                var NumReg = /\d+(\.\d{0,2})?/
                if (!NumReg.test(this.form2.movieprice)){
                    alert("表格填写不能为空!")
                    return;
                }
                let str = this.alltime.split(' ')[0]
                let str1 = str.slice(0, 4) + "年" + str.slice(5, 7) + "月" + str.slice(8, 10) + "日";
                // console.log(str1)
                this.form2.viewingdate = str1
                this.form2.showtime = this.alltime.split(' ')[1]
                // console.log(this.form2)
                this.axios.post("api/schedule/update", this.form2).then(res => {
                    if (res.data.sta == 'success') {
                        this.getData();
                        this.$message.success(res.data.msg);
                    } else {
                        this.$message.error(res.data.msg);
                    }
                });
                this.editVisible = false;
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
            selectmovie() {
                this.addform.movieid = this.movieDetailsList[this.addform.indexid].movieid
                this.addform.movieorigin = this.movieDetailsList[this.addform.indexid].moviedetails.movieorigin
                this.addform.leasedata = this.movieDetailsList[this.addform.indexid].moviedetails.moviereleasedata

            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
